package com.freecloud.framework.utils;

import cn.hutool.core.util.StrUtil;

import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.GpsDirectory;
import com.drew.metadata.file.FileSystemDirectory;
import com.drew.metadata.file.FileTypeDirectory;
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: maomao
 * @Date: 2019-08-17 17:42
 */
public class FileUtil extends cn.hutool.core.io.FileUtil {

    /**
     * 获取文件exif参数
     * @param fileObj
     * @return
     */
    public static Map<String,String> getFileExifMap(File fileObj)
    {
        if(fileObj == null)
        {
            return null;
        }

        Map<String,String> map = new HashMap<>();

        try
        {
            Metadata metadata = ImageMetadataReader.readMetadata(fileObj);

            //file
            Directory fileDirectory = metadata.getFirstDirectoryOfType (FileSystemDirectory.class);
            if(fileDirectory != null)
            {
                map.put ("file_name",fileDirectory.getDescription (FileSystemDirectory.TAG_FILE_NAME));
                map.put ("file_size",fileDirectory.getDescription (FileSystemDirectory.TAG_FILE_SIZE));
            }

            //fileType
            Directory fileTypeDirectory = metadata.getFirstDirectoryOfType (FileTypeDirectory.class);
            if(fileTypeDirectory != null)
            {
                map.put ("file_type",fileTypeDirectory.getDescription (FileTypeDirectory.TAG_DETECTED_FILE_MIME_TYPE));
                map.put ("file_extension",fileTypeDirectory.getDescription (FileTypeDirectory.TAG_EXPECTED_FILE_NAME_EXTENSION));
            }

            //Exif IFD0 Directory
            Directory IFD0Directory = metadata.getFirstDirectoryOfType (ExifIFD0Directory.class);
            if(IFD0Directory != null)
            {
                map.put ("make",IFD0Directory.getDescription (ExifIFD0Directory.TAG_MAKE));
                map.put ("model",IFD0Directory.getDescription (ExifIFD0Directory.TAG_MODEL));
                map.put ("date_time",IFD0Directory.getDescription (ExifIFD0Directory.TAG_DATETIME));
            }

            //GPS
            Directory gpsDirectory = metadata.getFirstDirectoryOfType (GpsDirectory.class);

            if(gpsDirectory != null)
            {
                if(StrUtil.isNotEmpty (gpsDirectory.getDescription (GpsDirectory.TAG_LATITUDE)))
                {
                    map.put ("gps_latitude", GPSUtil.convertToDegree (gpsDirectory.getDescription (GpsDirectory.TAG_LATITUDE)).toString ());
                }
                if(StrUtil.isNotEmpty (gpsDirectory.getDescription (GpsDirectory.TAG_LONGITUDE)))
                {
                    map.put ("gps_longitude",GPSUtil.convertToDegree (gpsDirectory.getDescription (GpsDirectory.TAG_LONGITUDE)).toString ());
                }
                if(StrUtil.isNotEmpty (gpsDirectory.getDescription (GpsDirectory.TAG_ALTITUDE)))
                {
                    map.put ("gps_altitude",gpsDirectory.getDescription (GpsDirectory.TAG_ALTITUDE));
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace ();
            return null;
        }

        return map;
    }
}
